package com.leetroll.app.controller;

import com.leetroll.api.service.SysConfigService;
import com.leetroll.common.bean.ApiResponse;
import com.leetroll.common.entity.SysConfig;
import com.leetroll.common.enums.EConfigKey;
import com.leetroll.common.utils.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RefreshScope
@RequestMapping("/config")
@Api(value = "配置接口", tags = {"配置接口"})
@Slf4j
public class SysConfigController {

    private final SysConfigService sysConfigService;

    @Autowired
    public SysConfigController(SysConfigService sysConfigService) {
        this.sysConfigService = sysConfigService;
    }

    @ApiOperation(value = "获取系统默认头像", notes = "获取系统默认头像")
    @GetMapping("/default_avatar")
    public ApiResponse<List<String>> listDefaultAvatar() {
        List<String> avatars = sysConfigService.listConfigFilesByKey(EConfigKey.DEFAULT_AVATAR);
        return ApiResponse.OK(avatars);
    }

    @ApiOperation(value = "获取系统默认背景", notes = "获取系统默认背景")
    @GetMapping("/default_bg")
    public ApiResponse<List<String>> listDefaultBgImg() {
        List<String> bgImgs = sysConfigService.listConfigFilesByKey(EConfigKey.DEFAULT_BG);
        return ApiResponse.OK(bgImgs);
    }

    @ApiOperation(value = "获取协议", notes = "获取协议")
    @GetMapping("/agreement")
    public ApiResponse<String> getAgreement(@RequestParam(name = "type") String type) {
        String result = sysConfigService.getByKey(type).getValue();
        return ApiResponse.OK(result);
    }

    @ApiOperation(value = "获取轮播图", notes = "获取轮播图")
    @GetMapping("/carousel")
    public ApiResponse<List<String>> getCarousel() {
        List<String> carousel = sysConfigService.listConfigFilesByKey(EConfigKey.CAROUSEL);
        return ApiResponse.OK(carousel);
    }


    @ApiOperation(value = "获取当前背景特效", notes = "获取当前背景特效")
    @GetMapping("/bg_effect")
    public ApiResponse<Map<String, Object>> getBgEffect() {
        SysConfig sysConfig = sysConfigService.getByKey(EConfigKey.CURRENT_EFFECT.getKey());
        if (sysConfig == null) return ApiResponse.OK();
        String json = sysConfig.getValue();
        Map<String, Object> map = JsonUtils.jsonToMap(json);
        return ApiResponse.OK(map);
    }
}
